<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="公司全称">
          <a-input placeholder="请选择公司" v-show v-decorator="['CompanyId', validatorRules.companyid]"></a-input>
          <a-input placeholder="请选择公司" @click="showCommonModal" v-decorator="['Company_CompanyName',{}]"></a-input>
          <!-- <a-select show-search :filter-option="filterOption" placeholder="请选择公司" @change="getVisitor" v-decorator="['CompanyId', validatorRules.companyid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.CompanyId" v-for="(item,index) in companyData">{{item.CompanyName}}</a-select-option>
          </a-select> -->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="服务日期">
          <a-date-picker placeholder="请输入服务日期" v-decorator="['ServiceDate', validatorRules.servicedate]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="服务标签">
          <a-select mode="tags" placeholder="请选择服务标签" v-decorator="['ServiceTagId', validatorRules.servicetagid]" >
            <a-select-option :value="item.Value" v-for="(item,index) in serviceTagData">{{item.Value}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="摘要">
          <a-input type="textarea" placeholder="请输入摘要" v-decorator="['Remarks', {}]" />
        </a-form-item>
        
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="服务状态">
          <a-select placeholder="请选择服务状态" v-decorator="['ServiceStatusId', validatorRules.servicestatusid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.BaseInfoId" v-for="(item,index) in serviceStatusData">{{item.Value}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="对接人">
          <a-select show-search :filter-option="filterOption" :disabled="editType=='detail'" mode="multiple" placeholder="请选择对接人" v-decorator="['Visitors', validatorRules.visitors]" >
            <a-select-option :value="item.PersonalInfo_PersonalName" v-for="(item,index) in visitorData">{{item.PersonalInfo_PersonalName}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="Linker">
          <a-select placeholder="请选择Linker" v-decorator="['LinkerId', validatorRules.linkerid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.Id" v-for="(item,index) in LinkData">{{item.Name}}</a-select-option>
          </a-select>
        </a-form-item>
		
      </a-form>
    </a-spin>
    <commonModal
      modalTitle="公司选择"
      ref="commonModal"
      @getCommonModalData="commonModalData"
      :columns="tableColumns"
      apiUrl="/companyInfo/queryRows"
      :searchArr="searchArr"></commonModal>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import commonModal from '@/components/commonModal'
  export default {
    name: "CompanyCooprationModal",
    components:{
      commonModal
    },
    data () {
      return {
        tableColumns:[
          { title: '公司全称', dataIndex: 'CompanyName' },
          { title: '公司编码', dataIndex: 'CompanyCode' },
        ],
        searchArr:[
          { label: '公司全称', key: 'CompanyName' },
          { label: '公司编码', key: 'CompanyCode' },
        ],
        visitorData:[],
        LinkData:[],
        serviceStatusData:[{
          id:'141',
          name:'服务成功'
        },{
          id:'142',
          name:'服务失败'
        },{
          id:'143',
          name:'对方违约'
        },{
          id:'144',
          name:'未服务'
        }],
        serviceTagData:[{
          id:'145',
          name:'招商宣传'
        },{
          id:'146',
          name:'数据服务'
        },{
          id:'147',
          name:'新媒体运营'
        },{
          id:'148',
          name:'创新榜单'
        },{
          id:'149',
          name:'峰会论坛'
        },{
          id:'150',
          name:'传播服务'
        },{
          id:'151',
          name:'投融资服务'
        },{
          id:'152',
          name:'资源对接'
        },{
          id:'153',
          name:'财税政策'
        },{
          id:'154',
          name:'线下活动'
        },{
          id:'155',
          name:'数据服务'
        }],
        personData:[],
        companyData:[],
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        companycooprationid:{rules: [{ required: true, message: '请输入合作信息ID!' }]},
        companyid:{rules: [{ required: true, message: '请选择公司!' }]},
        servicetagid:{rules: [{ required: true, message: '请选择服务标签!' }]},
        remarks:{rules: [{ required: true, message: '请输入摘要!' }]},
        servicedate:{rules: [{ required: true, message: '请输入服务日期!' }]},
        servicestatusid:{rules: [{ required: true, message: '请输入服务状态!' }]},
        visitors:{rules: [{ required: true, message: '请选择对接人!' }]},
        linkerid:{rules: [{ required: true, message: '请输入Linker!' }]},
        updatebyid:{rules: [{ required: true, message: '请输入最近更新人!' }]},
        ts:{rules: [{ required: true, message: '请输入最近更新日期!' }]},
        datastatus:{rules: [{ required: true, message: '请输入状态!' }]},
        },
        url: {
          add: "/companyCoopration/add",
          edit: "/companyCoopration/update",
          baseList:"/baseInfo/queryRows",
        },
      }
    },
    created () {
      this.getPerson();
      this.getLinker();
      // this.getCompany();
      this.getBaseData();
    },
    methods: {
      showCommonModal () {
        this.$refs.commonModal.open()
      },
      commonModalData(val){
        this.form.setFieldsValue({
          CompanyId:val[0].CompanyId,
          Company_CompanyName:val[0].CompanyName
        })
      },
      getVisitor(type){
        this.$nextTick(()=>{
          let data = this.form.getFieldsValue();
          if(!data.CompanyId){
            return false;
          }
          if(type !== 'init'){
            this.form.setFieldsValue({
              Visitors:[],
            })
          }
          this.$http.postAction('/companyStaff/queryRows',{pageNo:1,pageSize:9999,selectFields:[],conditions:[{
              "field":"Company",
              "operator":"=",
              "value":data.CompanyId
            }]}).then((res)=>{
            if(res.status == 1){
              this.visitorData = res.data.rows;
            }
          })
        })
        
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      getBaseData(){
        this.$http.postAction(this.url.baseList,{pageNo:1,pageSize:100,selectFields:[],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'ServiceTag'
            }]}).then((res)=>{
          if(res.status == 1){
            this.serviceTagData = res.data.rows;
          }
        })
        this.$http.postAction(this.url.baseList,{pageNo:1,pageSize:100,selectFields:[],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'ServiceStatus'
            }]}).then((res)=>{
          if(res.status == 1){
            this.serviceStatusData = res.data.rows;
          }
        })
      },
      getLinker(){
        this.$http.postAction('/user/queryRows',{pageNo:1,pageSize:9999,conditions:[{
              "field":"WorkStatus",
              "operator":"=",
              "value":'78'
            }]}).then((res)=>{
          if(res.status == 1){
            this.LinkData = res.data.rows;
          }
        })
      },
      getPerson(){
        this.$http.postAction('/personalInfo/queryRows',{pageNo:1,pageSize:9999}).then((res)=>{
          if(res.status == 1){
            this.personData = res.data.rows;
          }
        })
      },
      getCompany(){
        this.$http.postAction('/companyInfo/queryRows',{pageNo:1,pageSize:9999}).then((res)=>{
          if(res.status == 1){
            this.companyData = res.data.rows;
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.model.ServiceTagId = this.model.ServiceTag?this.model.ServiceTag.split(","):[];
        this.model.ServiceStatusId = this.model.ServiceStatus;
        this.model.LinkerId = this.model.Linker;
          this.model.CompanyId = this.model.Company;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'CompanyId','ServiceTagId','Remarks','ServiceStatusId','LinkerId','Visitors','Company_CompanyName'))
		  //时间格式化
      this.getVisitor('init');
      this.form.setFieldsValue({ServiceDate:this.model.ServiceDate?moment(this.model.ServiceDate,'YYYY-MM-DD HH:mm:ss'):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.CompanyCooprationId){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.Visitors = formData.Visitors instanceof Array?formData.Visitors.join(','):formData.Visitors;
            formData.ServiceDate = formData.ServiceDate?formData.ServiceDate.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.ServiceTagId = formData.ServiceTagId instanceof Array?formData.ServiceTagId.join():formData.ServiceTagId;
            console.log(formData)
            this.$http.postAction(httpurl,formData,method).then((res)=>{
              if(res.status == 1){
                that.$message.success('提交成功');
                if(method == 'post'){
                  this.form.resetFields();
                  this.model = {};
                }else{
                  that.close();
                }
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              // that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>